//
//  PopLoadMsg.m
//  FOA
//
//  Created by redoor4 on 15-9-17.
//  Copyright (c) 2015年 redoor4. All rights reserved.
//

#import "PopLoadMsg.h"
#import <QuartzCore/QuartzCore.h>


#define DELAYTIME       0.2 //网络请求200ms后显示进度
#define REMAINTIME      0.4 //至少显示500ms

@interface PopLoadMsg ()
{
    BOOL isRemoved;       //是否需要显示加载指示器
    BOOL isRemain;        //是否在延迟显示
    NSTimer * delayTimer; //网络请求后延迟加载定时器
    NSTimer * remainTimer;//延迟加载后短时间就不显示时需要维护一段时间，避免闪现
}

@end

@implementation PopLoadMsg
@synthesize imageview;

- (instancetype)init
{
    if (self = [super init])
    {
        isRemain  = YES;
    }
    
    return self;
}


//加载图片操作
-(void)loadImages{
    UIWindow *WINDOW=[UIApplication sharedApplication].keyWindow;
   
    imageview=[[UIImageView alloc]initWithFrame:CGRectMake(30*W, (SCREEN_HEIGHT-50*H)/2, SCREEN_WIDTH-60*W, 50*H)];
    imageview.backgroundColor=[UIColor whiteColor];
    
    
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(15, 10*H, 30*H, 30*H)];
    image.image=[UIImage imageNamed:@"refreshicon"];
    
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue=[NSNumber numberWithFloat:0.f];
    animation.toValue=[NSNumber numberWithFloat:2*M_PI];
    animation.duration=2.0f;
    animation.repeatCount=1000;
   
    [image.layer addAnimation:animation forKey:@"start"];

    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(60*H, 15*H, 100, 20*H)];
    lab.text=NSLocalizedString(@"Loading...", nil);
    
    lab.font=[UIFont systemFontOfSize:14];
    lab.textColor=[UIColor blackColor];
    [imageview addSubview:lab];
    [imageview addSubview:image];
    [WINDOW addSubview:imageview];
}

/**
 *  网络加载指示器延迟加载delayLoadImages与移除delayImagesRemove成对出现
 */
- (void)delayLoadImages
{
    isRemain  = YES;
    delayTimer = [NSTimer scheduledTimerWithTimeInterval:DELAYTIME target:self selector:@selector(delayTimerHandle:) userInfo:nil repeats:NO];
}

- (void)delayImagesRemove
{
    /**
     *  延迟显示结束后再移除指示器
     */
    if (!isRemain)
    {
        [self.imageview removeFromSuperview];
    }
    
    isRemoved = YES;
}

/**
 *  网络请求一定时间后显示加载进度
 *
 *  @param _timer <#_timer description#>
 */
- (void)delayTimerHandle:(NSTimer *) _timer
{
    if (!isRemoved)
    {
        UIWindow *WINDOW=[UIApplication sharedApplication].keyWindow;
        
        imageview=[[UIImageView alloc]initWithFrame:CGRectMake(30*W, (SCREEN_HEIGHT-50*H)/2, SCREEN_WIDTH-60*W, 50*H)];
        imageview.backgroundColor=[UIColor whiteColor];
        
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(15, 10*H, 30*H, 30*H)];
        image.image=[UIImage imageNamed:@"refreshicon"];
        
        CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.fromValue=[NSNumber numberWithFloat:0.f];
        animation.toValue=[NSNumber numberWithFloat:2*M_PI];
        animation.duration=2.0f;
        animation.repeatCount=1000;
        
        [image.layer addAnimation:animation forKey:@"start"];
        
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(60*H, 15*H, 100, 20*H)];
        lab.text=NSLocalizedString(@"Loading...", nil);
        
        lab.font=[UIFont systemFontOfSize:14];
        lab.textColor=[UIColor blackColor];
        [imageview addSubview:lab];
        [imageview addSubview:image];
        [WINDOW addSubview:imageview];
        
        /**
         *  至少显示一段时间，避免闪现
         */
        isRemain = YES;//正在延迟显示过程中
        remainTimer = [NSTimer scheduledTimerWithTimeInterval:REMAINTIME target:self selector:@selector(remainTimerHandle:) userInfo:nil repeats:NO];
    }
    [delayTimer invalidate];
    delayTimer = nil;
}

/**
 *  显示一段时间后消失，避免闪现
 *
 *  @param _timer <#_timer description#>
 */
-(void)remainTimerHandle:(NSTimer *) _timer
{
    [remainTimer invalidate];
    remainTimer = nil;
    isRemain = NO;//延迟显示结束
    
    if (isRemoved)
    {
        [self.imageview removeFromSuperview];
    }
}

@end
